<template>
  <section class="page" v-remainScroll="'branchTopicDetail'">
    <headerbar>{{title}}
    </headerbar>

    <section>
      <section class="detail">
        <h1 class="title text-left" v-html="tools.resumeEnter(data.title)"></h1>
        <div class="text-content" v-html="tools.resumeEnter(data.content)"></div>
      </section>
      <section class="detail">
        <div class="detail-my">
          <div class="left"><span>全部</span><span>&nbsp;(&nbsp;{{count}}&nbsp;)&nbsp;</span></div>
          <div class="right" @click.stop="addTopic"><div class="icon-topic"></div><span>参与话题</span></div>
        </div>
      </section>
      <section class="mgt-2 pdh-2">
        <fetchData
          class="live-msg-wrap pdv-4"
          :loading="isLoading"
          :data="selectList"
        ></fetchData>
        <div
          class="block live-msg-wrap study-my"
          @click="goMemorabiliasDetail(item, 4)"
          v-for="(item, index) in selectList"
          :key="index"
        >
          <div class="user-avatar">
            <div class="avatar">
              <img
                v-if="item.createUser"
                :src="tools.getAvatarUrl(item.createUser)+'&'+Date.now()"
                alt=""
              />
              <img
                v-else=""
                src="../../../../static/images/user_bg.jpg"
                alt=""
              />
            </div>
            <div class="info">
              <div class="head-my">
                <div>{{ item.createUserName }}</div>
                <div v-if="isMySelf(item)" @click.stop="del(item.id,index)" class="right">删除</div>
              </div>
              <li class="time-my">{{ getTimeNew(item.createTime) }} · 参与了话题</li>
            </div>
          </div>
          <div class="live-msg" v-html="tools.resumeEnter(item.title)"></div>
          <div class="card-foot-my">
            <span class="span-my" v-html="tools.resumeEnter(item.content)">
            </span>
            <p class="text">
              <span>送花&nbsp;{{ item.supportTotal }}</span><span>评论&nbsp;{{ item.commentTotal }}</span>
            </p>
          </div>
        </div>
        <section class="people-list">
          <div
            class="text-center no-data"
            :loading="isLoading"
            :data="selectList"
            v-if="!isLoading && selectList.length === 0"
          >
            <img src="../../../../static/images/dangjian/home/water-logo-comment.png" alt="">
          </div>
        </section>
      </section>
    </section>
  </section>
</template>
<script>
  import api from '@/common/config/api'
  import tools from "@/utils/tools";
  import moment from 'moment'
  export default {
    name: 'branchTopicDetail',
    data () {
      return {
        data: {},
        count:0,
        title: '话题',
        selectList:[],
        isLoading: true,
        userInfo:'',
      }
    },
    beforeRouteLeave(to, from, next) {
      this.$route.meta.keepAlive = to.name === 'branchExperienceDetail'
      if(this.$route.meta.keepAlive) {
        this.$store.commit('addRoute','branchTopicDetail')
      }else {
        this.$store.commit('removeRoute','branchTopicDetail')
        this.$destroy()
      }
      next()
    },
    beforeRouteEnter(to, from, next) {
      next((vm) => {
        if (to.name === "branchTopicDetail") {
          to.meta.keepAlive = true;
          vm.$store.commit("addRoute", "branchTopicDetail");
        vm.loadData()
        }
      });
    },
    created () {
      this.userInfo = this.$store.state.userInfo;
      this.data = this.$store.state.topicDetail
      this.loadData()
    },
    methods: {
      loadData () {
        this.isLoading =true
        let data = {
          pageNo: "1",
          pageSize: "200",
          topicId: this.data.id
        }
        this.fetchService.topic_selectListByTopic(data).then(res => {
          this.isLoading = false
          if (res.code !== 0) {
            return this.tools.tip(res.message)
          }
          this.count = res.count
          this.selectList = res.data
        })
      },
      addTopic(){
        this.$router.push({ name: 'learningExperiencesAdd' ,query:{topicId:this.data.id},params:{title:this.data.title}});
      },
      getTimeNew(createTime) {
        if(new Date(createTime).getFullYear()<new Date().getFullYear()){
          return moment(createTime).format("YYYY-M-D");
        }else if(new Date(createTime).getDay()==new Date().getDay()){
          return moment(createTime).format("HH:mm:ss");
        }
        return moment(createTime).format("MM-DD");
      },
      goMemorabiliasDetail(item){
        console.log('item',item);
        this.$router.push({ name: 'branchExperienceDetail' ,params:{data:item}});
      },
      isMySelf(item){//是否为自己
        if(item.createUser==this.userInfo.id){
          return true
        }
        return false
      },
      del(id,index){
        let _this = this
        this.$createDialog({
          type: 'confirm',
          title: "温馨提示",
          content: '确认删除？',
          confirmBtn: {
            text: '确定',
            active: true,
            disabled: false,
            href: 'javascript:;'
          },
          cancelBtn: {
            text: '取消',
            active: false,
            disabled: false,
            href: 'javascript:;'
          },
          onConfirm: () => {
            _this.deleteMaterial(id,index);
          },
          onCancel: () => {
          }
        }).show()
      },
      deleteMaterial(id,index){
        this.fetchService.topic_delXd(id).then(res => {
          if (res.code !== 0) {
            return this.tools.tip(res.message)
          }
          this.tools.tip("删除成功")
          this.count --
          this.selectList.splice(index, 1)
        })
      },
    }
  }
</script>

<style scoped lang="scss">
 @import "@/common/stylus/moon.scss";
  .page{
    background: #fff !important;
    font-family: dang,PingFang SC,miui,system-ui,-apple-system,BlinkMacSystemFont,Helvetica Neue,Helvetica,sans-serif !important;
  }
  .detail{
    padding: 0.3rem;
    border-bottom: 1px solid #d0d0d0;
    .title{
      user-select:text;
      padding: 0px;
      font-weight: normal;
      font-size: 16PX;
      word-wrap: break-word;
      word-break: normal;
    }
    .detail-my{
     display: flex;
      justify-content: space-between;
      align-items: center;
      .left{
        //margin-left: 3%;
        font-size: 16PX;
        >span{
          margin-right: 15px;
        }
      }
      .right{
        margin-right: 3%;
        display: flex;
        font-size: 16PX;
        border: 1px solid #e20001;
        padding: 3px;
        border-radius: 5%;
        padding: 1%;
        .icon-topic{
          display: inline-block;
          width: 0.6rem;
          height: 0.5rem;
          background: url('../../../../static/images/dangjian/home/icon-topic.png') no-repeat center;
          background-size: 100% 100%;
          margin-right: 0.13333rem;
        }
        >span{
          height: 0.6rem;
          line-height: 0.6rem;
          font-size: 16PX;
          color: #e20001;
        }
      }
    }
    .text-content{
      font-size: 16PX;
      text-align: justify;
    }
  }
  .enclosure-upload p {
    font-size:16PX;
    text-align:justify;
  }
  .list-item {
    flex-wrap:wrap;
    .enclosure-upload {
      width:33%;
      margin-right:.5%;
      margin-bottom:.5%;
      flex-shrink:0;
      &:nth-child(3n + 3) {
        margin-right:0
      }
      img {
        width:100% !important;
        min-height: 50px;
        height:auto !important;
      }
    }
  }
 .live-msg-wrap .user-avatar {
   padding: 0.17rem;
   .info {
     flex: 1;
     h1 {
       font-size: 16PX;
     }
   }
   .no-wrap {
     align-self: flex-start;
     padding-top: 10px;
   }
 }
 .people-list .people-item .user-avatar .color-tip {
   font-size: 16PX;
 }
 .people-list .people-item .name {
   line-height: inherit;
 }
 .live-msg {
   overflow: hidden;
   -webkit-line-clamp: 2;
   text-overflow: ellipsis;
   max-height: 4.5em;
   -webkit-box-orient: vertical;
   display: -webkit-box;
   font-weight: bold;
   font-size: 16PX;
 }
 .study-my{
   padding: 3px;
   background: #fff;
   -webkit-box-shadow: 0px 0.06667rem 0.26667rem 0px rgba(0,0,59,0.1);
   box-shadow: 0px 0.06667rem 0.26667rem 0px rgba(0,0,59,0.1);
   overflow: hidden;
   margin: 0 0 3px 0;
   border-radius: 0px;
   .card-foot-my {
     padding-left: 0.2rem;
     padding-right: 0.2rem;
     .span-my{
       user-select:text;
       font-weight: normal;
       overflow: hidden;
       text-overflow: ellipsis;
       display: -webkit-box;
       -webkit-box-orient: vertical;
       -webkit-line-clamp: 2;
       word-break: break-all;
       font-size: 16PX;
       color: #333;
       font-family: PingFang SC,miui,system-ui,-apple-system,BlinkMacSystemFont,Helvetica Neue,Helvetica,sans-serif !important;
     }
     .text{
       font-size: 12PX;
       margin: 0.2rem 0;
       color: #999999;
       >span{
         margin-right: 50px;
       }
     }
   }
 }
 .head-my {
   font-size: 16PX;
   display: flex;
   justify-content: space-between;
   width: 100%;
   margin: 0.2rem 0;
   .right{
     margin-right: 4%;
     color: #D81E06;
     .icon-delete{
       display: inline-block;
       background: url('../../../../static/images/dangjian/home/icon-delete-red.png') no-repeat center;
       width: 0.4rem;
       height: 0.4rem;
       background-size: 100% 100%;
       margin-right: 0.13333rem;
     }
   }
 }
 .time-my{
   font-size: 12PX;
   color:#999;;
 }
</style>
